﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using Game.Objects;
using Game.Types;

namespace Game.Editor.Toolbox
{
    public class InfrastructureItemControl2 : UserControl
    {
        readonly Button _Button = new Button();
        readonly Image _Image = new Image();

        ObjectType _ObjectType;

        public InfrastructureItemControl2()
        {
            InitializeComponent();

            SelectedInfrastructure.Selected += delegate
            {
                this.UpdatePressedState();
            };
        }

        void InitializeComponent()
        {
            Grid grid = new Grid();
            this.Content = grid;

            Canvas canvas = new Canvas();
            grid.Children.Add(canvas);

            canvas.Children.Add(_Button);
            _Button.Click += Button_Click;
            _Button.Content = _Image;
        }

        public Brush ToolColor
        {
            get { return _Button.Background; }
            set { _Button.Background = value; }
        }

        public ObjectType ObjectType
        {
            get { return _ObjectType; }
            set { _ObjectType = value; DisplayType(); }
        }

        void DisplayType()
        {
            if (_ObjectType != null)
            {
                _Image.Source = _ObjectType.Picture;

                ToolTip t = new ToolTip();
                t.Content = "" + _ObjectType.Name;
                ToolTipService.SetToolTip(_Button, t);
            }
        }

        void Button_Click(object sender, RoutedEventArgs e)
        {
            SelectedInfrastructure.ObjectType = _ObjectType;
        }

        void UpdatePressedState()
        {
            if (SelectedInfrastructure.ObjectType == _ObjectType)
            {
                _Button.Background = new SolidColorBrush(SystemColors.HighlightColor);
            }
            else
            {
                _Button.Background = new SolidColorBrush(SystemColors.ControlLightColor);
            }
        }

    }
}
